/*
 * Copyright (C) 2017 Pingan, Inc. All Rights Reserved.
 */

package com.pingan.haofang.searchcloud.web.admin.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.pingan.haofang.gov.sm.account.common.JsonResult;
import com.pingan.haofang.gov.sm.account.entity.domain.Account;
import com.pingan.haofang.gov.sm.account.entity.domain.Role;
import com.pingan.haofang.gov.sm.account.entity.dto.account.AccountDTO;
import com.pingan.haofang.gov.sm.account.entity.dto.account.AccountInfoDTO;
import com.pingan.haofang.gov.sm.account.entity.dto.account.BaseAccountDTO;
import com.pingan.haofang.gov.sm.account.entity.enums.FaceValidateStatusEnum;
import com.pingan.haofang.gov.sm.account.entity.enums.NameValidateStatusEnum;
import com.pingan.haofang.gov.sm.account.service.account.IAccountInfoService;
import com.pingan.haofang.gov.sm.account.service.account.IAccountService;
import com.pingan.haofang.gov.sm.account.service.role.IRoleService;
import com.pingan.haofang.searchcloud.user.service.SearchUserService;
import com.pingan.haofang.searchcloud.web.admin.form.AddAccountForm;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 管理端接口
 *
 * @author zhangbi617
 * @date 22/05/2017
 */
@RestController
@RequestMapping("/systemAdmin")
@Api(tags = "System.Admin", description = "管理接口")
public class AdminController {
	@Autowired
	private IAccountService accountService;

	@Autowired
	private IAccountInfoService accountInfoService;

	@Autowired
	private IRoleService roleService;
	
	@Autowired
	SearchUserService searchUserService;

	@ApiOperation("给用户分配角色")
	@PostMapping(value = "/role")
	public JsonResult<Role> bindRoleForAccount(@RequestParam(value = "accountId") Long accountId) {
		return JsonResult.successResponse(roleService.bindRoleForAccount(accountId, 1L));
	}

	@ApiOperation(value = "新增用户(跳过注册)")
	@PostMapping(value = "/add")
	public JsonResult<BaseAccountDTO> addAccount(@RequestBody AddAccountForm form) {
		String mobile = form.getMobile();

		AccountDTO accountDTO = new AccountDTO();
		accountDTO.setSourceType(2);
		accountDTO.setIdentity("");
		accountDTO.setPassword(form.getPassword());
		accountDTO.setMobile(mobile);
		accountDTO.setUsername(mobile);
		accountDTO.setName(mobile);
		Account account = accountService.addAccount(accountDTO);

		AccountInfoDTO accountInfoDTO = new AccountInfoDTO();
		accountInfoDTO.setEmail(form.getEmail());
		accountInfoDTO.setAccountId(account.getId());
		accountInfoDTO.setFaceVerificationStatus(FaceValidateStatusEnum.PASS.getValue());
		accountInfoDTO.setNameVerificationStatus(NameValidateStatusEnum.PASS.getValue());
		accountInfoService.addAccountInfo(accountInfoDTO);

		Long roleId = 1L;// 普通用户角色id，初始化数据
		roleService.bindRoleForAccount(account.getId(), roleId);

		BaseAccountDTO baseAccountDTO = new BaseAccountDTO();
		baseAccountDTO.setName(account.getName());
		baseAccountDTO.setPassword(form.getPassword());
		baseAccountDTO.setUsername(account.getUsername());
		return JsonResult.successResponse(baseAccountDTO);
	}
}
